﻿#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
// 普通函数
void printHello(const string& name) {
    for (int i = 0; i < 3; ++i) {
        cout << "Hello from function, " << name << endl;
        this_thread::sleep_for(chrono::milliseconds(100));
    }
}
// 类中静态函数
class MyClass {
public:
    static void staticFunc(int count) {
        for (int i = 0; i < count; ++i) {
            cout << "Static member function: " << i << endl;
            this_thread::sleep_for(chrono::milliseconds(100));
        }
    }
    void memberFunc() {
        for (int i = 0; i < 3; ++i) {
            cout << "Non-static member function" << endl;
            this_thread::sleep_for(chrono::milliseconds(100));
        }
    }
};

int main() {
    MyClass obj;
    // 1. 使用普通函数
    thread t1(printHello, "Alice");
    // 2. 使用静态成员函数
    thread t2(&MyClass::staticFunc, 5);
    // 3. 使用非静态成员函数（需要对象指针）
    thread t3(&MyClass::memberFunc, &obj);
    // 4. 使用 lambda 表达式
    thread t4([] {
        for (int i = 0; i < 3; ++i) {
            cout << "Lambda thread executing" << endl;
            this_thread::sleep_for(chrono::milliseconds(100));
        }
    });
    // 等待所有线程结束
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    cout << "Main thread finished." << endl;
    return 0;
}
